package com.ljfresh.customer.utils.base;

import com.ljfresh.customer.utils.FileUtils;
import okhttp3.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhuosr on 2018/5/21.
 */
@SuppressWarnings("deprecation")
public class HttpClientUtil {

    public static final MediaType JSON = MediaType.get("application/json;charset=UTF-8");
    public static final MediaType FORM = MediaType.get("application/x-www-form-urlencoded;charset=UTF-8");
    public static final MediaType FORM2312 = MediaType.get("application/x-www-form-urlencoded;charset=GB2312");
    public static final MediaType STREAM = MediaType.get("application/octet-stream");
    public static final MediaType MEDIA_TYPE_JPEG= MediaType.get("image/jpeg");
    public static final MediaType MEDIA_TYPE_GIF = MediaType.get("image/gif");
    public static final MediaType MEDIA_TYPE_PNG = MediaType.get("image/png");


    private static OkHttpClient client;

    static {
        client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();
    }

    //get请求
    public static String getApiJson(String url, String param) throws IOException {
        Request request = new Request.Builder()
                .url(url+"?"+param)
                .get()
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    //get请求(有路径参数)
    public static String getApiJson(String urlPart1,Long pathParam, String urlPart2,String param) throws IOException {
        Request request = new Request.Builder()
                .url(urlPart1+pathParam+urlPart2+"?"+param)
                .get()
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    //api请求  json数据方式提交
    public static String getApiJsonForHttpsJsonType(String url, String param,String Object) throws IOException {
        RequestBody body = RequestBody.create(JSON, Object);
        Request request = new Request.Builder()
                .url(url+"?"+param)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    //api请求   form数据方式提交
    public static String getApiJsonForHttpsFormType(String url, String param) throws IOException {
        RequestBody body = RequestBody.create(FORM, param);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    //api请求  GB2312编码  凌凯短信用
    public static String getApiJsonForGB2312(String url, String param) {
//        return "";
        RequestBody body = RequestBody.create(FORM2312, param);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //api请求 上传文件
    public static String getApiForUpload(String url, String param, MultipartFile multipartFile, String fileName) {

        try {
            //step 2:创建 RequestBody 以及所需的参数
            //2.1 获取文件
            File file = new File(fileName);
            //2.2 创建 MediaType 设置上传文件类型
            //2.3 获取请求体
            RequestBody requestBody = RequestBody.create(STREAM, FileUtils.multipartFileToFile(multipartFile));

            //step 3：创建请求
            Request request = new Request.Builder().url(url+"?"+param)
                    .post(requestBody)
                    .build();
            Response response = null;
                response =client.newCall(request).execute();
            return response.body().string();

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    //api请求 上传文件
    public static String getApiForUpload(String url, String param, File file, String fileName) {

        try {
            //step 2:创建 RequestBody 以及所需的参数
            //2.1 获取文件
            //2.2 创建 MediaType 设置上传文件类型
            //2.3 获取请求体
            RequestBody requestBody = RequestBody.create(STREAM, file);

            //step 3：创建请求
            Request request = new Request.Builder().url(url+"?"+param)
                    .post(requestBody)
                    .build();
            Response response = null;
            response =client.newCall(request).execute();
            return response.body().string();

        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }


}

